package com.sdruixinggroup.mondayb2b.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.sdruixinggroup.mondayb2b.API.API;
import com.sdruixinggroup.mondayb2b.R;
import com.sdruixinggroup.mondayb2b.interfaces.ObjectCallBack;
import com.sdruixinggroup.mondayb2b.models.ModelBeen;
import com.sdruixinggroup.mondayb2b.ui.Activities.LoginActivity;
import com.sdruixinggroup.mondayb2b.utils.UserInfoUtil;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * Created by tangjie on 2017/11/11.
 */

public class CommitPassWord extends BaseActivity {
    @BindView(R.id.et_old_psw)
    EditText etOldPsw;
    @BindView(R.id.et_new_psd)
    EditText etNewPsd;
    @BindView(R.id.et_new_psd_again)
    EditText etNewPsdAgain;
    @BindView(R.id.bt_finish)
    Button btFinish;

    private ImageView topbar_left;
    private TextView topbar_center;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_commitpsd);
        ButterKnife.bind(this);
        topbar_center = (TextView) findViewById(R.id.topbar_title);
        topbar_center.setText("修改密码");
        topbar_left = (ImageView) findViewById(R.id.topbar_left);
        topbar_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @OnClick(R.id.bt_finish)
    public void onClick() {
        queryData(etOldPsw.getText().toString().trim(), etNewPsd.getText().toString().trim(), etNewPsdAgain.getText().toString().trim());
    }

    private void queryData(String origin, String password, String verify) {
        String url = API.PASSWORD + "origin=" + origin + "&password=" + password + "&verify=" + verify + "&access_token=" + UserInfoUtil.getInfoToken(getApplicationContext());
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Accept", "*/*");

        TypeToken<ModelBeen> typeToken = new TypeToken<ModelBeen>() {
        };
        OkHttpUtils.post().headers(headers).url(url).build().execute(new ObjectCallBack<ModelBeen>(typeToken) {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(ModelBeen response, int id) {
                if (response.err_code == 0) {
                    showToast("修改成功，请重新 登录");
                    UserInfoUtil.saveToken(getApplicationContext(), "");
                    Intent inmain = new Intent();
                    inmain.setClass(CommitPassWord.this, LoginActivity.class);
                    inmain.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    inmain.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(inmain);
                    finish();
                } else {
                    showToast(response.err_msg);
                }
            }
        });
    }


}
